<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>vue-源码笔记-初始化方法(2) | lafen | lafen&#39;s personal blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="learning,note,vue">
    <meta name="description" content="写在前面继续往下写，但是 vue 的初始化过程经过实在很多，还是挑一些重点记录一下比较好 上一篇讲了 vue 在初始化做的一些操作，主要是给 Vue 构造函数和 Vue 原型对象挂载一些方法，比如 $set 、 $del、 $on、$once、$off，这一篇继续往下看还有哪些方法被挂载，并看看这些方法的实现 $forceUpdate在需要的地方，我们可能会用到 this.$forceUpdate">
<meta name="keywords" content="learning,note,vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue-源码笔记-初始化方法(2)">
<meta property="og:url" content="https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/index.html">
<meta property="og:site_name" content="lafen">
<meta property="og:description" content="写在前面继续往下写，但是 vue 的初始化过程经过实在很多，还是挑一些重点记录一下比较好 上一篇讲了 vue 在初始化做的一些操作，主要是给 Vue 构造函数和 Vue 原型对象挂载一些方法，比如 $set 、 $del、 $on、$once、$off，这一篇继续往下看还有哪些方法被挂载，并看看这些方法的实现 $forceUpdate在需要的地方，我们可能会用到 this.$forceUpdate">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-12-03T03:00:38.374Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue-源码笔记-初始化方法(2)">
<meta name="twitter:description" content="写在前面继续往下写，但是 vue 的初始化过程经过实在很多，还是挑一些重点记录一下比较好 上一篇讲了 vue 在初始化做的一些操作，主要是给 Vue 构造函数和 Vue 原型对象挂载一些方法，比如 $set 、 $del、 $on、$once、$off，这一篇继续往下看还有哪些方法被挂载，并看看这些方法的实现 $forceUpdate在需要的地方，我们可能会用到 this.$forceUpdate">
    
        <link rel="alternate" type="application/atom+xml" title="lafen" href="/atom.xml">
    
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide">
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">lafen</h5>
          <a href="mailto:1031822863@qq.com" title="1031822863@qq.com" class="mail">1031822863@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/">
                <i class="icon icon-lg icon-home"></i>
                home
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives">
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags">
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories">
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/zhongxiaotian" target="_blank">
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://www.weibo.com/" target="_blank">
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://gitee.com/lafen" target="_blank">
                <i class="icon icon-lg icon-link"></i>
                Gitee
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">vue-源码笔记-初始化方法(2)</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">vue-源码笔记-初始化方法(2)</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-12-03T02:59:36.000Z" itemprop="datePublished" class="page-time">
  2019-12-03
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/">vue</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/learning/">learning</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/learning/note/">note</a></li></ul></li></ul></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#写在前面"><span class="post-toc-number">1.</span> <span class="post-toc-text">写在前面</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#forceUpdate"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">$forceUpdate</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#destory"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">$destory</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#nextTick"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">$nextTick</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2" class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">vue-源码笔记-初始化方法(2)</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-12-03 10:59:36" datetime="2019-12-03T02:59:36.000Z" itemprop="datePublished">2019-12-03</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/">vue</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/learning/">learning</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/vue/learning/note/">note</a></li></ul></li></ul></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style="display:none">
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h3 id="写在前面"><a href="#写在前面" class="headerlink" title="写在前面"></a>写在前面</h3><p>继续往下写，但是 vue 的初始化过程经过实在很多，还是挑一些重点记录一下比较好</p>
<p>上一篇讲了 <code>vue</code> 在初始化做的一些操作，主要是给 <code>Vue</code> 构造函数和 <code>Vue</code> 原型对象挂载一些方法，比如 <code>$set</code> 、 <code>$del</code>、 <code>$on</code>、<code>$once</code>、<code>$off</code>，这一篇继续往下看还有哪些方法被挂载，并看看这些方法的实现</p>
<h4 id="forceUpdate"><a href="#forceUpdate" class="headerlink" title="$forceUpdate"></a>$forceUpdate</h4><p>在需要的地方，我们可能会用到 <code>this.$forceUpdate</code> 来进行强制刷新视图</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$forceUpdate = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">var</span> vm = <span class="keyword">this</span>;</span><br><span class="line">      <span class="keyword">if</span> (vm._watcher) &#123;</span><br><span class="line">        vm._watcher.update();</span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>其实，里面就调用了以下观察者的 <code>update()</code> 方法，让其去比对视图是否需要更新。</p>
<h4 id="destory"><a href="#destory" class="headerlink" title="$destory"></a>$destory</h4><p>顾名思义，这是销毁一个实例的方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$destroy = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">var</span> vm = <span class="keyword">this</span>;</span><br><span class="line">      <span class="comment">// 避免重复销毁，正在销毁时为 true</span></span><br><span class="line">      <span class="keyword">if</span> (vm._isBeingDestroyed) &#123;</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 先触发 beforeDestroy 钩子</span></span><br><span class="line">      callHook(vm, <span class="string">'beforeDestroy'</span>);</span><br><span class="line">      vm._isBeingDestroyed = <span class="literal">true</span>;</span><br><span class="line">      <span class="comment">// remove self from parent</span></span><br><span class="line">      <span class="comment">// 将组件从其父组件的 $children 中移除自己</span></span><br><span class="line">      <span class="keyword">var</span> parent = vm.$parent;</span><br><span class="line">      <span class="keyword">if</span> (parent &amp;&amp; !parent._isBeingDestroyed &amp;&amp; !vm.$options.abstract) &#123;</span><br><span class="line">        remove(parent.$children, vm);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// teardown watchers</span></span><br><span class="line">      <span class="comment">// 移除观察者</span></span><br><span class="line">      <span class="keyword">if</span> (vm._watcher) &#123;</span><br><span class="line">        vm._watcher.teardown();</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 移除通过 $watch 来进行监听的观察者</span></span><br><span class="line">      <span class="keyword">var</span> i = vm._watchers.length;</span><br><span class="line">      <span class="keyword">while</span> (i--) &#123;</span><br><span class="line">        vm._watchers[i].teardown();</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// remove reference from data ob</span></span><br><span class="line">      <span class="comment">// frozen object may not have observer.</span></span><br><span class="line">      <span class="keyword">if</span> (vm._data.__ob__) &#123;</span><br><span class="line">        vm._data.__ob__.vmCount--;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// call the last hook...</span></span><br><span class="line">      vm._isDestroyed = <span class="literal">true</span>;</span><br><span class="line">      <span class="comment">// invoke destroy hooks on current rendered tree</span></span><br><span class="line">      vm.__patch__(vm._vnode, <span class="literal">null</span>);</span><br><span class="line">      <span class="comment">// fire destroyed hook</span></span><br><span class="line">      <span class="comment">// 触发 destroyed 钩子</span></span><br><span class="line">      callHook(vm, <span class="string">'destroyed'</span>);</span><br><span class="line">      <span class="comment">// turn off all instance listeners.</span></span><br><span class="line">      <span class="comment">// 解除所有事件监听</span></span><br><span class="line">      vm.$off();</span><br><span class="line">      <span class="comment">// remove __vue__ reference</span></span><br><span class="line">      <span class="keyword">if</span> (vm.$el) &#123;</span><br><span class="line">        vm.$el.__vue__ = <span class="literal">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// release circular reference (#6759)</span></span><br><span class="line">      <span class="keyword">if</span> (vm.$vnode) &#123;</span><br><span class="line">        vm.$vnode.parent = <span class="literal">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>前面根据标志来判断是够正在销毁实例，避免重复销毁实例</li>
<li>首先立即触发 <code>beforeDestroy</code> 钩子，将销毁标志设置为 <code>true</code></li>
<li>然后解除当前实例在父实例中的引用</li>
<li>移除自身收集的观察者 <code>_watcher</code></li>
<li><p>移除用户自己设置的观察者 <code>_watchers</code></p>
</li>
<li><p>触发最后的 <code>destroyed</code> 钩子</p>
</li>
<li>调用 <code>vm.$off()</code> 解除所有事件监听</li>
<li>最后一个解除自身对父实例的循环引用</li>
</ul>
<h4 id="nextTick"><a href="#nextTick" class="headerlink" title="$nextTick"></a>$nextTick</h4><p>这个方法使用的也不算多，因为 <code>Vue</code> 中更改 <code>dom</code> 后更新视图操作是异步的，但当我们有时候立即获取 <code>dom</code> 时，此时的 <code>dom</code> 是还未更新的，这时候就可以使用 <code>$nextTick</code> 来保证获取到的是更新后的 <code>dom</code></p>
<p><code>$nextTick</code> 在 <code>renderMixin(Vue)</code> 中挂载到原型上</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.$nextTick = <span class="function"><span class="keyword">function</span> (<span class="params">fn</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">return</span> nextTick(fn, <span class="keyword">this</span>)</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>看来具体实现在 <code>nextTick</code> 中，搜索到此方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// nextTick 方法</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">nextTick</span> (<span class="params">cb, ctx</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> _resolve;</span><br><span class="line">    <span class="comment">// 将回调存入队列中 callbacks</span></span><br><span class="line">    callbacks.push(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">if</span> (cb) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">          cb.call(ctx);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">          handleError(e, ctx, <span class="string">'nextTick'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (_resolve) &#123;</span><br><span class="line">        _resolve(ctx);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">if</span> (!pending) &#123;</span><br><span class="line">      pending = <span class="literal">true</span>;</span><br><span class="line">      timerFunc(); <span class="comment">// 执行</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// $flow-disable-line</span></span><br><span class="line">    <span class="keyword">if</span> (!cb &amp;&amp; <span class="keyword">typeof</span> <span class="built_in">Promise</span> !== <span class="string">'undefined'</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span> (<span class="params">resolve</span>) </span>&#123;</span><br><span class="line">        _resolve = resolve; <span class="comment">// 将resolve回调赋值给_resolve</span></span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>看到传入的 <code>cb</code> 回调函数并没有直接执行，而是使用匿名函数包装之后，压入一个执行队列 <code>callbacks</code> 中保存，往下看当等待状态 <code>pending</code> 为 <code>false</code> 时候，执行了 <code>timerFunc</code>，看看这是什么函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> timerFunc;</span><br><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="built_in">Promise</span> !== <span class="string">'undefined'</span> &amp;&amp; isNative(<span class="built_in">Promise</span>)) &#123;</span><br><span class="line">    <span class="comment">// 优先使用 promise 来触发宏任务</span></span><br><span class="line">    <span class="keyword">var</span> p = <span class="built_in">Promise</span>.resolve();</span><br><span class="line">    <span class="comment">// timerFunc 赋值</span></span><br><span class="line">    timerFunc = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      p.then(flushCallbacks);</span><br><span class="line">      <span class="comment">// 强制刷新微任务队列</span></span><br><span class="line">      <span class="keyword">if</span> (isIOS) &#123; setTimeout(noop); &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    isUsingMicroTask = <span class="literal">true</span>; <span class="comment">// 使用微任务标志</span></span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (!isIE &amp;&amp; <span class="keyword">typeof</span> MutationObserver !== <span class="string">'undefined'</span> &amp;&amp; (</span><br><span class="line">    <span class="comment">// 如无 promise ，则判断是否有 MutationObserver</span></span><br><span class="line">    isNative(MutationObserver) ||</span><br><span class="line">    <span class="comment">// PhantomJS and iOS 7.x</span></span><br><span class="line">    MutationObserver.toString() === <span class="string">'[object MutationObserverConstructor]'</span></span><br><span class="line">  )) &#123;</span><br><span class="line">    <span class="comment">// 无 promise 时使用MutationObserver</span></span><br><span class="line">    <span class="keyword">var</span> counter = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">var</span> observer = <span class="keyword">new</span> MutationObserver(flushCallbacks);</span><br><span class="line">    <span class="keyword">var</span> textNode = <span class="built_in">document</span>.createTextNode(<span class="built_in">String</span>(counter));</span><br><span class="line">    observer.observe(textNode, &#123;</span><br><span class="line">      characterData: <span class="literal">true</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// timerFunc 赋值</span></span><br><span class="line">    timerFunc = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      counter = (counter + <span class="number">1</span>) % <span class="number">2</span>;</span><br><span class="line">      textNode.data = <span class="built_in">String</span>(counter);</span><br><span class="line">    &#125;;</span><br><span class="line">    isUsingMicroTask = <span class="literal">true</span>; <span class="comment">// 使用微任务标志</span></span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> setImmediate !== <span class="string">'undefined'</span> &amp;&amp; isNative(setImmediate)) &#123;</span><br><span class="line">    <span class="comment">// promise MutationObserver 都没有，则降级为宏任务</span></span><br><span class="line">    <span class="comment">// 先使用 setImmediate 将回调添加进宏任务队列</span></span><br><span class="line">    <span class="comment">// 如果再没有 setImmediate，则只能使用setTimeout</span></span><br><span class="line">    timerFunc = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      setImmediate(flushCallbacks); <span class="comment">// flushCallbacks 作为回调</span></span><br><span class="line">    &#125;;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 降级为宏任务 setTimeout</span></span><br><span class="line">    timerFunc = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      setTimeout(flushCallbacks, <span class="number">0</span>); <span class="comment">// flushCallbacks 作为回调</span></span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>看到这部分代码根据不同情况，给 <code>timerFunc</code> 赋值了不同的函数，<code>promise</code>、<code>MutationObserver</code>、<code>setImmediate</code> 与 <code>setTimeout</code> ，依次按顺序赋值给 <code>timerFunc</code>，而回调函数中传入 <code>flushCallbacks</code> ，从顺序可以看出， <code>Vue</code> 会首先使用<strong>微任务</strong>注册回调，当环境不支持的时候才使用<strong>宏任务</strong>，宏任务和微任务可以看这篇<a href="[https://lafen.gitee.io/2019/09/27/js-%E5%AE%8F%E4%BB%BB%E5%8A%A1%E5%92%8C%E5%BE%AE%E4%BB%BB%E5%8A%A1/js-%E5%AE%8F%E4%BB%BB%E5%8A%A1%E5%92%8C%E5%BE%AE%E4%BB%BB%E5%8A%A1/](https://lafen.gitee.io/2019/09/27/js-宏任务和微任务/js-宏任务和微任务/">《js-宏任务和微任务》</a> )</p>
<p>接下来看看注册的回调函数 <code>flushCallbacks</code> 里面干了什么</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 是否使用微任务</span></span><br><span class="line"><span class="keyword">var</span> isUsingMicroTask = <span class="literal">false</span>;</span><br><span class="line"><span class="comment">// 任务队列</span></span><br><span class="line"><span class="keyword">var</span> callbacks = [];</span><br><span class="line"><span class="keyword">var</span> pending = <span class="literal">false</span>;</span><br><span class="line"><span class="comment">// 重置 pending状态， 取出回调函数执行</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flushCallbacks</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    pending = <span class="literal">false</span>; <span class="comment">// pedding 状态</span></span><br><span class="line">    <span class="keyword">var</span> copies = callbacks.slice(<span class="number">0</span>); <span class="comment">// 从任务队列中取出所有回调执行</span></span><br><span class="line">    callbacks.length = <span class="number">0</span>; <span class="comment">// 清空任务队列</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; copies.length; i++) &#123;</span><br><span class="line">      copies[i](); <span class="comment">// 执行所有回调</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> <code>flushCallbacks</code> 中只是从任务队列 <code>callbacks</code> 中取出所有的回调函数，依次执行而已，至此，这就是 <code>nextTick</code> 的全部代码了</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-12-03T03:00:38.374Z" itemprop="dateUpdated">2019-12-03 11:00:38</time>
</span><br>


        
        关于学习类文章网上已经有非常多，而此处文章都是我在学习过程中整理摘录的笔记，参考文章可以点击文末的参考链接进行查阅
        
    </div>
    
    <footer>
        <a href="https://lafen.gitee.io">
            <img src="/img/avatar.jpg" alt="lafen">
            lafen
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/learning/">learning</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/note/">note</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue/">vue</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/&title=《vue-源码笔记-初始化方法(2)》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/&title=《vue-源码笔记-初始化方法(2)》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《vue-源码笔记-初始化方法(2)》 — lafen&url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/12/11/算法初探-冒泡排序及优化/算法初探-冒泡排序及优化/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">算法初探-冒泡排序及优化</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">vue-源码笔记-初始化方法分析</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        打笔巨款？~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check" data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style="display:none">
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style="display:none">
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>lafen &copy; 2019 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/&title=《vue-源码笔记-初始化方法(2)》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/&title=《vue-源码笔记-初始化方法(2)》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《vue-源码笔记-初始化方法(2)》 — lafen&url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/12/03/vue-源码笔记-初始化方法-2/vue-源码笔记-初始化方法-2/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"positon":"right","width":150,"height":300},"mobile":{"show":true,"scale":0.8},"react":{"opacity":0.8,"opacityDefault":0.8,"opacityOnHover":0.6},"log":false});</script></body>
</html>
